package com.sfac.modules.common.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfac.modules.common.mapper.ExceptionLogMapper;
import com.sfac.modules.common.pojo.ExceptionLog;
import com.sfac.modules.common.service.ExceptionLogService;
import com.sfac.modules.common.vo.Result;
import com.sfac.modules.common.vo.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:唐琳的爹
 * @BelongsProject:hqyj_springboot
 * @BelongsPackage:com.sfac.modules.common.serviceimpl
 * @CreateTime:2022/8/25 09:49:12
 */
@Service
public class ExceptionLogImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;


    /**
     * 根据path和exceptiontype查询异常日志
     * @param path
     * @param exceptionType
     * @return
     */
    @Override
    public ExceptionLog getElByPathAndName(String path, String exceptionType) {
        LambdaQueryWrapper<ExceptionLog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(path),ExceptionLog::getPath,path)
           .eq(StringUtils.isNotBlank(exceptionType),ExceptionLog::getExceptionType,exceptionType);
        return exceptionLogMapper.selectOne(lqw);
    }

    /**
     * 添加日常日志
     * @param exceptionLog
     * @return
     */
    @Override
    @Transactional
    public Result<ExceptionLog> insertExceptionLog(ExceptionLog exceptionLog) {
        LambdaQueryWrapper<ExceptionLog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ExceptionLog::getPath,exceptionLog.getPath())
                .eq(ExceptionLog::getExceptionType,exceptionLog.getExceptionType());
        if (exceptionLogMapper.selectOne(lqw) != null){
            return new Result<ExceptionLog>(StatusCode.SUCCESS,"异常信息已存在");
        }
        exceptionLog.setCreateDate(LocalDateTime.now());
        exceptionLog.setUpdateDate(LocalDateTime.now());
        exceptionLogMapper.insert(exceptionLog);
        return new Result<>(StatusCode.SUCCESS,"添加成功!");
    }
}
